import {
  dynastyList
} from "../../../utils/commondata.js";

import {
  request
} from "../../../utils/request.js";
import regeneratorRuntime from '../../../utils/runtime';

var appInst =  getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    authorInfo:{},
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    let options = currentPage.options;
    const { authorId ,isShare} = options;
    let share = isShare?true:false;
    this.setData({
      isShare : share,
      authorId: authorId,
    });
    this.loadAuthorInfo(authorId);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let shareObj = {
      title: this.data.authorInfo.name,
      path: '/pages/author/detail/index?isShare=true&authorId='+this.data.authorId,

    };
    return shareObj;
  },
  handleMore(e){
    let {index} = e.currentTarget.dataset;
    let contentList = [];

    for(let i=0;i<this.data.authorInfo.introduceVOList.length;i++){
      let c = this.data.authorInfo.introduceVOList[i];
      if(i == index){
        c.height = 500;
      }else{
        c.height = 10;
      }
      contentList.push(c);
    }
    let authorInfo = this.data.authorInfo;
    authorInfo.introduceVOList = contentList;
    this.setData({
      authorInfo:authorInfo
    });
  },
  handleLess(e){
    let contentList = [];
    for(let i=0;i<this.data.authorInfo.introduceVOList.length;i++){
      let c = this.data.authorInfo.introduceVOList[i];
      c.height = 10;
      contentList.push(c);
    }
    let authorInfo = this.data.authorInfo;
    authorInfo.introduceVOList = contentList;
    this.setData({
      authorInfo:authorInfo
    });
  },

  /**
   * 加载作者信息
   * 
   * @param {*} authorId 
   */
  async loadAuthorInfo(authorId){
    const res = await request({
      url: "/author/detail?id=" + authorId,
      method: "get"
    });
    if(res.code != 0){
      return;
    }


    console.log(res.data);
    res.data.introduceVOList.forEach(element => {
      element.height=10
    });
    this.setData({
      authorInfo:res.data
    });
  },


})